/*
package com.tarena.lbs.gateway.filter;
import com.alibaba.fastjson2.JSON;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.passport.commom.encoder.JwtEncoder;
import com.tarena.lbs.passport.protocol.jwt.UserPrinciple;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class AuthenticationFilter implements GlobalFilter {
    private static final String AUTH_HEADER="Authorization";
    @Autowired(required = false)
    private JwtEncoder<UserPrinciple> jwtEncoder;

    @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //get jwt
        log.info("AuthenticationFilter filter into");
        String jwt = getJwt(exchange);
        UserPrinciple userPrincipal=null;
        try {
            userPrincipal= jwtEncoder.getLoginFromToken(jwt, UserPrinciple.class);
        } catch (Exception e) {
            return Mono.error(e);
        }
        if (userPrincipal!=null){
            setUserJsonHeader(exchange,chain,userPrincipal);
        }
        return chain.filter(exchange);
    }

    private void setUserJsonHeader(ServerWebExchange exchange, GatewayFilterChain chain, UserPrinciple userPrinciple) {
        log.info("AuthenticationFilter setUserJsonHeader into");
        String userJson = JSON.toJSONString(userPrinciple);
        LinkedHashMap<String, String> headers = new LinkedHashMap<>();
        headers.put(AUTH_HEADER, userJson);
        Consumer<HttpHeaders> httpHeaders = httpHeader -> {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                // 遍历Map设置header，向后传递
                httpHeader.set(entry.getKey(), entry.getValue());
            }
        };
        ServerHttpRequest newRequest = exchange.getRequest().mutate().headers(httpHeaders).build();
        ServerWebExchange build = exchange.mutate().request(newRequest).build();
        chain.filter(build);
    }

    private String getJwt(ServerWebExchange exchange){
        HttpHeaders headers = exchange.getRequest().getHeaders();
        boolean authorization = headers.containsKey(AUTH_HEADER);
        List<String> auths = headers.get(AUTH_HEADER);
        log.info("auth header contains:{}", auths == null ? null : auths);
        if (authorization && CollectionUtils.isNotEmpty(auths)) {
            log.debug("Auth header value:{}",headers.get(AUTH_HEADER));
            return headers.get(AUTH_HEADER).get(0);
        } else {
            log.debug("jwt is not contained in Authorization header");
            return null;
        }
    }
}
*/
